package com.kawaiior.asoulworld.entity.entityattack;


import com.kawaiior.asoulworld.item.ModItems;
import com.kawaiior.asoulworld.potion.ModPotions;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class EntityHolyArrow extends EntityASArrow {

    public EntityHolyArrow(World worldIn) {
        super(worldIn);
    }

    public EntityHolyArrow(World worldIn, EntityLivingBase shooter) {
        super(worldIn, shooter);
    }

    @Override
    protected ItemStack getArrowStack() {
        return new ItemStack(ModItems.DIANA_CANDY);
    }

    @Override
    public void onUpdate() {
        super.onUpdate();
        if (world.isRemote && !inGround) {
            int stateId = Block.getStateId(Blocks.GOLD_BLOCK.getDefaultState());
            for (int i = 0; i < 4; ++i) {
                this.world.spawnParticle(EnumParticleTypes.FALLING_DUST, this.posX + this.motionX * (double) i / 4.0D, this.posY + this.motionY * (double) i / 4.0D, this.posZ + this.motionZ * (double) i / 4.0D, -this.motionX, -this.motionY + 0.2D, -this.motionZ, stateId);
            }
        }
    }

    @Override
    protected void onHit(RayTraceResult ray) {
        super.onHit(ray);
        if (!world.isRemote && ray.entityHit instanceof EntityLivingBase) {
            EntityLivingBase entityLivingBase = (EntityLivingBase) getThrower();
            EntityLivingBase entityLiving = ((EntityLivingBase) ray.entityHit);

            if (entityLiving instanceof EntityPlayer) {
                world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX,entityLiving.posY,entityLiving.posZ,true));
                world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX+2,entityLiving.posY,entityLiving.posZ+2,true));
                world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX-2,entityLiving.posY,entityLiving.posZ-2,true));
                world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX+2,entityLiving.posY,entityLiving.posZ-2,true));
                world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX-2,entityLiving.posY,entityLiving.posZ+2,true));
                world.createExplosion(entityLivingBase, entityLiving.posX, entityLiving.posY+2, entityLiving.posZ, 2, false);
                EntityUtil.ApplyBuff(entityLiving, ModPotions.AS_BREAK_DEFENSE, 0, 10f);
                return;
            }

            entityLiving.attackEntityFrom(DamageSource.causeIndirectMagicDamage(entityLivingBase, entityLivingBase).setDamageBypassesArmor(), 30f);
            world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX,entityLiving.posY,entityLiving.posZ,true));
            world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX+2,entityLiving.posY,entityLiving.posZ+2,true));
            world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX-2,entityLiving.posY,entityLiving.posZ-2,true));
            world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX+2,entityLiving.posY,entityLiving.posZ-2,true));
            world.addWeatherEffect((Entity)new EntityLightningBolt(world,entityLiving.posX-2,entityLiving.posY,entityLiving.posZ+2,true));
            world.createExplosion(entityLivingBase, entityLiving.posX, entityLiving.posY+2, entityLiving.posZ, 3, false);
        }
    }
}
